package com.joy.educms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.joy.commonutils.R;
import com.joy.educms.entity.CrmBanner;
import com.joy.educms.service.CrmBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 后台的首页banner表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-12-06
 */
@RestController
@RequestMapping("/educms/banneradmin")
@CrossOrigin
public class BannerAdminController {

    @Autowired
    private CrmBannerService crmBannerService;

    //1、分页查询banner
    @GetMapping("/pageBanner/{page}/{limit}")
    public R pageBanner(@PathVariable long page,@PathVariable long limit){
        Page<CrmBanner> pageBanner = new Page<CrmBanner>(page,limit);
        crmBannerService.page(pageBanner,null);

        long total = pageBanner.getTotal();
        List<CrmBanner> records = pageBanner.getRecords();
        return R.ok().data("total",total).data("records",records);
    }

    //2、添加banner
    @PostMapping("/addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner){
        crmBannerService.save(crmBanner);
        return R.ok();
    }

    //3、修改banner
    @PutMapping("/updateBanner")
    public R updateBanner(@RequestBody CrmBanner crmBanner){
        crmBannerService.updateById(crmBanner);
        return R.ok();
    }

    //4、删除banner
    @DeleteMapping("/deleteBanner/{id}")
    public R deleteBanner(@PathVariable String id){
        crmBannerService.removeById(id);
        return R.ok();
    }

    //5、根据id查询
    @PostMapping("/selectBannerById/{id}")
    public R selectBannerById(@PathVariable String id){
        CrmBanner crmBanner = crmBannerService.getById(id);
        return R.ok().data("item",crmBanner);
    }


}

